package main

import (
	"SkyPath/internal/adapter/web"
	"SkyPath/internal/infrastructure"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"log"
)

func main() {
	// 初始化数据库
	err := infrastructure.CreateDB()
	if err != nil {
		log.Fatalln("init db table construct failed, exit.")
		return
	}

	// grpc 加载，初步实现加密插件，验证是否存在插件文件，如果存在则验证加密

	// 初始化 Gin
	app := gin.Default()
	// 配置 CORS 中间件
	app.Use(cors.New(cors.Config{
		AllowOrigins:     []string{"http://localhost:8000"}, // 允许的前端地址
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowHeaders:     []string{"Origin", "Content-Type", "Accept", "Authorization"},
		AllowCredentials: true,
	}))

	groups := map[string]string{
		"dbtool":  "/skypath/web/dbtool",
		"user":    "/skypath/web/user",
		"project": "/skypath/web/project",
		"ledger":  "/skypath/web/ledger",
	}
	for _, path := range groups {
		group := app.Group(path)
		group.GET("/v1/getDistribute", func(c *gin.Context) {
			c.Header("Content-Type", "application/json")
			web.GetDistribute(c)
		})
	}
	// 创建省级项目
	for _, path := range groups {
		group := app.Group(path)
		group.POST("/v1/createProject", func(c *gin.Context) {
			c.Header("Content-Type", "application/json")
			web.CreateProject(c)
		})
	}
	// 创建省级项目下某个系统项目条目信息
	for _, path := range groups {
		group := app.Group(path)
		group.POST("/v1/createProjectItem", func(c *gin.Context) {
			c.Header("Content-Type", "application/json")
			web.CreateProjectItems(c)
		})
	}
	// 查询省级项目
	for _, path := range groups {
		group := app.Group(path)
		group.GET("/v1/getProject", func(c *gin.Context) {
			c.Header("Content-Type", "application/json")
			web.GetProject(c)
		})
	}
	// 查询省级下某个系统条目信息
	for _, path := range groups {
		group := app.Group(path)
		group.GET("/v1/getProjectItem", func(c *gin.Context) {
			c.Header("Content-Type", "application/json")
			web.GetProjectItem(c)
		})
	}
	// 查询省级下某个系统条目信息的具体详情
	for _, path := range groups {
		group := app.Group(path)
		group.GET("/v1/getProjectItemDetail", func(c *gin.Context) {
			c.Header("Content-Type", "application/json")
			web.GetProjectItemDetail(c)
		})
	}
	// 修改某个省级项目
	for _, path := range groups {
		group := app.Group(path)
		group.POST("/v1/changeProject", func(c *gin.Context) {
			c.Header("Content-Type", "application/json")
			web.ChangeProject(c)
		})
	}
	// 修改省级下某个系统条目信息
	for _, path := range groups {
		group := app.Group(path)
		group.POST("/v1/changeProjectItem", func(c *gin.Context) {
			c.Header("Content-Type", "application/json")
			web.ChangeProjectItem(c)
		})
	}
	// 删除省级
	for _, path := range groups {
		group := app.Group(path)
		group.POST("/v1/delProject", func(c *gin.Context) {
			c.Header("Content-Type", "application/json")
			web.DeleteProject(c)
		})
	}
	// 删除省级下某个系统条目
	for _, path := range groups {
		group := app.Group(path)
		group.POST("/v1/delProjectItem", func(c *gin.Context) {
			c.Header("Content-Type", "application/json")
			web.DeleteProjectItem(c)
		})
	}

	// 获取台账表头
	for _, path := range groups {
		group := app.Group(path)
		group.GET("/v1/getLedgerHeaders", func(c *gin.Context) {
			c.Header("Content-Type", "application/json")
			c.Header("Access-Control-Allow-Origin", "*")
			web.GetLedgerHeaders(c)
		})
	}
	// 获取台账
	for _, path := range groups {
		group := app.Group(path)
		group.GET("/v1/getLedgers", func(c *gin.Context) {
			c.Header("Content-Type", "application/json")
			c.Header("Access-Control-Allow-Origin", "*")
			web.GetLedger(c)
		})
	}
	// 更改台账
	for _, path := range groups {
		group := app.Group(path)
		group.POST("/v1/changeLedgers", func(c *gin.Context) {
			c.Header("Content-Type", "application/json")
			//c.Header("Access-Control-Allow-Origin", "*")
			web.ChangeLedgers(c)
		})
	}

	// 上传文件
	for _, path := range groups {
		group := app.Group(path)
		group.GET("/v1/upload", func(c *gin.Context) {
			c.Header("Content-Type", "application/json")
			web.UploadFiles(c)
		})
	}
	// 下载文件
	for _, path := range groups {
		group := app.Group(path)
		group.GET("/v1/download", func(c *gin.Context) {
			c.Header("Content-Type", "application/json")
			web.DownloadFiles(c)
		})
	}

	// 启动服务器
	app.Run(":8080")

}
